package com.example.springbatchdemo.future;


import java.util.Optional;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @Classname CompletedFutureAction
 * @Description TODO
 * @Date 2020/9/26 19:37
 * @Created by kevin
 */
public class CompletableFutureAction {

    private final static Random RANNDOM = new Random(System.currentTimeMillis());

    public static void main( String[] args ) {
        CompletableFuture<Double> completableFuture = new CompletableFuture<>();
        new Thread(() -> {
            double value = getDouble();
            completableFuture.complete(value);


        }).start();
        System.out.println("这是直接返回得信息！！！！！！");

        completableFuture.whenComplete(( k, v ) -> {
            Optional.ofNullable(k).ifPresent(System.out::println);
            Optional.ofNullable(v).ifPresent(x -> x.printStackTrace());

        });
    }

    private static double getDouble() {

        try {
            TimeUnit.MILLISECONDS.sleep(RANNDOM.nextInt(10000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        return RANNDOM.nextDouble();
    }


}
